﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using log4net;
using System.Text;
using AMebooking.NetUtils.Settings;
using AMebooking.NetUtils;
using AMebooking.Database;
using AMebooking.Models;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace AMebooking.Service
{
    public partial class Resources : System.Web.UI.Page
    {
        protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected static readonly StringBuilder sb = new StringBuilder();


        protected void Page_Load(object sender, EventArgs e)
        {
            var type = Request.QueryString["type"];
            var idProperty = Request.QueryString["idProperty"];
            var siteid = Request.QueryString["siteid"];

            var parsedData = "";
            var resources = "";

            var ret = new List<Mechanic>();
            ret = DBAccess.getMechanic("", "", siteid);
            if (ret.Count > 0)
            {
                for (int i = 0; i < ret.Count; )
                {
                    var obj = "";
                    obj = ((Mechanic)ret[i]).ToString();
                    resources += obj;
                    if (++i < ret.Count)
                    {
                        resources += ",";
                    }
                }
                resources = string.Format("[{0}]", resources);
            }

            var departments = "";
            var retDepts = new List<Department>();
            retDepts = DBAccess.getDepartment();
            if (retDepts.Count > 0)
            {
                for (int i = 0; i < retDepts.Count; )
                {
                    var obj = retDepts[i].ToString();
                    departments += obj;
                    if (++i < retDepts.Count)
                    {
                        departments += ",";
                    }
                }
                departments = string.Format("[{0}]", departments);
            }

            if ("resources".Equals(type))
            {
                parsedData = resources;
            }
            else if ("departments".Equals(type))
            {
                parsedData = departments;
            }
            else if ("shifts".Equals(type))
            {
                parsedData = "[]";
            }
            else if ("settings".Equals(type))
            {
                var appConfig = AppConfig.GetCustomSettings("ebookingSettings/appSettings");
                var map = new Hashtable();
                foreach (var key in appConfig.Keys)
                {
                    map[key] = appConfig[key as string];
                }
                parsedData = JsonConvert.SerializeObject(map);
            }
            else if ("initd".Equals(type))
            {
                parsedData = "{"
                            + "\"resources\":" + resources
                            + ", \"departments\": " + departments
                            + @"}";
            }

            var json = parsedData;
            Page.Response.ContentType = "application/json";
            Page.Response.Write(json);
            Page.Response.End();
        }
    }
}
